<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\Order;

class OrderController extends Controller {
	private $user_id;
	/**
	 * Create a new controller instance.
	 *
	 * @return void
	 */
	public function __construct() {
		if (\Auth::guard('api')->check()) {
			$user = \Auth::guard('api')->user();
			$this->user_id = $user->id;
		}
	}
	//单个订单获取详情
	public function get($id) {
		$detail = DB::table('orders')->find($id);
		if (empty($detail)) {
			return $this->respond('get_error');
		}
		$data['orderInfo'] = $detail;
		$data['invoiceInfo'] = [];
		$invoice = DB::table('invoices')->where('orderId', $detail->id)->first();
		$invoice && $data['invoiceInfo'] = $invoice;
		return $this->respond('get_ok', $data);
	}
	//所有订单获取详情
	public function all(Request $request) {
        //修改超过一天没有支付的订单状态
		$check_order = DB::table('orders as a')->where(['a.userId' => $this->user_id, 'status' => 1])->select('id', 'createdAt')->orderBy('id','desc')->get();
		if (!empty($check_order)) {
			foreach ($check_order as $key => $value) {
				$get_order_date = strtotime("+1 day", $value->createdAt);
				if (time() >= $get_order_date) {
					Order::where('id', $value->id)->update(['status' => 3]);
				}
                /*else{
                    break;
                }*/
			}
		}
		$page = $this->page($request);
		$query = DB::table('orders as a')->where('a.userId', $this->user_id);
		$searchVal = $request->input('searchVal');
		if (!empty($searchVal)) {
			$query->where(function ($query) use ($searchVal) {
				$query->orwhere('a.orderNo', '=', $searchVal);
				$query->orwhere('a.name', 'like', '%' . $searchVal . '%');
			});
		}
		$total_item = $query->orderBy('id', 'desc')->get();
		if (empty($total_item)) {
			return $this->respond('get_ok', ['item' => [], 'count' => 0, 'pageNum' => 0]);
		}
		$item = array_slice($total_item->toArray(), $page['skip'], $page['take']);
		$count = count($total_item);
		$data['count'] = $count;
		$pageNum = ceil($count / $page['take']);
		$data['pageNum'] = $pageNum;
		$data['item'] = $item;
		return $this->respond('get_ok', $data);
	}
	//所有订单导出excel
	public function export(Request $request) {
		$query = DB::table('orders as a')->where(['a.userId' => $this->user_id]);
		$searchVal = $request->input('searchVal');
		if (!empty($searchVal)) {
			$query->where(function ($query) use ($searchVal) {
				$query->orwhere('a.orderNo', '=', $searchVal);
				$query->orwhere('a.name', 'like', '%' . $searchVal . '%');
			});
		}
		$list = $query->get();
		if (!count($list)) {
			return $this->respond('get_error');
		}
		$cellData = [];
		$i = 1;
		$cellData = [['版本名称', '订单号', '购买数量', '购买时长', '订单价格', '支付类型', '支付状态', '创建时间', '支付时间']];
		$title = '订单记录_' . time();
		foreach ($list as $key => $val) {
			$from = ($val->type == 1) ? '微信' : '支付宝';
			$status = ($val->status == 1) ? '未支付' : '已支付';
			$cellData[$i] = [$val->name, $val->orderNo, $val->num . '个', $val->term . '年', $val->price, $from, $status, date('Y年m月d日 H:i:s', $val->createdAt), date('Y年m月d日 H:i:s', $val->updatedAt)];
			$i++;
		}
		Excel::create($title, function ($excel) use ($cellData) {
			$excel->sheet('order', function ($sheet) use ($cellData) {
				$sheet->rows($cellData);
			});
		})->store('xls', 'uploads/excel');
		$path['filepath'] = 'uploads/excel/' . $title . '.xls';
		return $this->respond('get_ok', $path);
	}
}
